package cn.wolfcode.shop.seckillserver.web.controller.frontend;

import cn.wolfcode.shop.common.util.CodeMsg;
import cn.wolfcode.shop.common.util.Result;
import cn.wolfcode.shop.member.domain.User;
import cn.wolfcode.shop.redis.RedisService;
import cn.wolfcode.shop.seckillserver.anno.UserParam;
import cn.wolfcode.shop.seckillserver.redis.SeckillKeyPrefix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * UUID生成唯一路径 SECKILL_PATH 生命周期为1秒
 * 用来隐藏用户真实访问路径
 */
@RestController
@RequestMapping("/path")
public class PathController {

    @Autowired
    private RedisService redisService;

    @RequestMapping("/get")
    public Result getPath(Long goodId, Integer verifyCode, String uuid, @UserParam User user){

        if(user==null){
            return Result.error(CodeMsg.LOGIN_ERROR);
        }

        //判断验证码正确
        Integer result = redisService.get(SeckillKeyPrefix.VERIFYCODE_RESULT, uuid, Integer.class);
        if(result==null || result != verifyCode){
            return Result.error(CodeMsg.VERIFYCODE_ERROR);
        }

        String path = UUID.randomUUID().toString().replace("-","");
        //将UUID存入缓存
        redisService.set(SeckillKeyPrefix.SECKILL_PATH,user.getId()+":"+goodId,path);

        //返回给页面,由页面异步携带访问 /{PHTH}/doSeckill
        return Result.success(path);
    }
}
